/*******************************************************************************
 * Copyright (c) 2012 March and Conquest.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package mrusanov.towers.main.ui.stats;

import java.util.List;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import mrusanov.towers.stats.GameStatistics;
import mrusanov.towers.stats.PlayersPair;

public class GameStatisticsTreeModelAdapter implements TreeModel {

	private final GameStatisticsRoot root;

	public GameStatisticsTreeModelAdapter() {
		root = new GameStatisticsRoot();
	}

	@Override
	public Object getRoot() {
		return root;
	}

	@Override
	public Object getChild(Object parent, int index) {
		if (parent instanceof GameStatisticsRoot) {
			GameStatisticsRoot root = (GameStatisticsRoot) parent;
			return root.getPairs().get(index);
		}

		if (parent instanceof List) {
			List<?> list = (List<?>) parent;
			return list.get(index);
		}

		if (parent instanceof PlayersPair) {
			PlayersPair pair = (PlayersPair) parent;
			return pair.getGames().get(index);
		}

		throw new IllegalArgumentException("Incorrect object class in tree: " + parent.getClass());
	}

	@Override
	public int getChildCount(Object parent) {
		if (parent instanceof GameStatisticsRoot) {
			GameStatisticsRoot root = (GameStatisticsRoot) parent;
			return root.getPairs().size();
		}

		if (parent instanceof List) {
			List<?> list = (List<?>) parent;
			return list.size();
		}

		if (parent instanceof PlayersPair) {
			PlayersPair pair = (PlayersPair) parent;
			return pair.getGames().size();
		}

		if (parent instanceof GameStatistics) {
			return 0;
		}

		throw new IllegalArgumentException("Incorrect object class in tree: " + parent.getClass());
	}

	@Override
	public boolean isLeaf(Object node) {
		return node instanceof GameStatistics;
	}

	@Override
	public void valueForPathChanged(TreePath path, Object newValue) {
		// statistics cann't be changed manually
	}

	@Override
	public int getIndexOfChild(Object parent, Object child) {
		for (int i = 0; i < getChildCount(parent); i++) {
			if (child == getChild(parent, i)) {
				return i;
			}
		}

		return -1;
	}

	@Override
	public void addTreeModelListener(TreeModelListener l) {
		// TODO Auto-generated method stub

	}

	@Override
	public void removeTreeModelListener(TreeModelListener l) {
		// TODO Auto-generated method stub

	}
}
